package br.com.hotstar.model.DAO.ORM;


import java.util.List;

import javax.inject.Inject;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import br.com.hotstar.model.DAO.GenericDAO;
import br.com.hotstar.model.entity.Usuario;

/**
 *
 * @author henrique
 */
class SuperDAO<T> implements GenericDAO<T>{
	
	@Inject
    protected Session sessao;
   
   
    public void save(T e){

		this.sessao.save(e);

	}

	public void delete(T e){
		this.sessao.flush();
		this.sessao.clear();
		this.sessao.delete(e);
	}

	public void update(T e){
		this.sessao.flush();
		this.sessao.clear();
		this.sessao.update(e);
	
	}

	public Object carregar(Class<?> c ,int id) {
		
		return this.sessao.get(c, id);
		
	}

	@Override
	public Object obterEntidadePorPropriedade(String nome, Object value) {
		Criteria crit = sessao.createCriteria(Usuario.class);
		crit.add(Restrictions.eq(nome,value));
		
		List l = crit.list();
		if(l.size() > 0)
			throw new RuntimeException();
		else if(l.size() == 0)
			return null;
		else{
			return l.get(0);
		}
	}
	

}

